/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#include "ti_msp_dl_config.h"
#include "main.h"


uint8_t oled_buffer[32];

int main(void) {

  SYSCFG_DL_init();
  SysTick_Init();
  // 清除串口中断标志
  NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN);
  // 使能串口中断
  NVIC_EnableIRQ(UART_0_INST_INT_IRQN);

  MPU6050_Init();
  OLED_Init();

  OLED_ShowString(0, 7, (uint8_t *)"MPU6050 Demo", 8);

  OLED_ShowString(0, 0, (uint8_t *)"Pitch", 8);
  OLED_ShowString(0, 2, (uint8_t *)" Roll", 8);
  OLED_ShowString(0, 4, (uint8_t *)"  Yaw", 8);

  OLED_ShowString(16 * 6, 3, (uint8_t *)"Accel", 8);
  OLED_ShowString(17 * 6, 4, (uint8_t *)"Gyro", 8);


  //MPU6050_calibrate();  
  while (1) {
    sprintf((char *)oled_buffer, "%-6.1f", pitch);
    OLED_ShowString(5 * 8, 0, oled_buffer, 16);
    sprintf((char *)oled_buffer, "%-6.1f", roll);
    OLED_ShowString(5 * 8, 2, oled_buffer, 16);
    sprintf((char *)oled_buffer, "%-6.1f", yaw);
    OLED_ShowString(5 * 8, 4, oled_buffer, 16);

    sprintf((char *)oled_buffer, "%6d", accel[0]);
    OLED_ShowString(15 * 6, 0, oled_buffer, 8);
    sprintf((char *)oled_buffer, "%6d", accel[1]);
    OLED_ShowString(15 * 6, 1, oled_buffer, 8);
    sprintf((char *)oled_buffer, "%6d", accel[2]);
    OLED_ShowString(15 * 6, 2, oled_buffer, 8);

    sprintf((char *)oled_buffer, "%6d", gyro[0]);
    OLED_ShowString(15 * 6, 5, oled_buffer, 8);
    sprintf((char *)oled_buffer, "%6d", gyro[1]);
    OLED_ShowString(15 * 6, 6, oled_buffer, 8);
    sprintf((char *)oled_buffer, "%6d", gyro[2]);
    OLED_ShowString(15 * 6, 7, oled_buffer, 8);

    //uart0_printf("Gyro X: %d, Gyro Y: %d, Gyro Z: %d\r\n", gyro[0], gyro[1], gyro[2]);

    //delay_ms(100); // 延时100毫秒
  }
}

void UART_0_INST_IRQHandler(void) {
  // 如果产生了串口中断
  switch (DL_UART_getPendingInterrupt(UART_0_INST)) {
  case DL_UART_IIDX_RX: // 如果是接收中断
    // 将发送过来的数据保存在变量中
    uart_data = DL_UART_Main_receiveData(UART_0_INST);
    // 将保存的数据再发送出去
    uart0_send_char(uart_data);
    break;

  default: // 其他的串口中断
    break;
  }
}
